package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Roles;
import org.hibernate.Session;
import com.raitcon.hibernate.factory.HibernateUtil;
import org.hibernate.Query;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

public class RolesDB {

    Session session = null;

    public RolesDB() {

        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List getRolesAll() {
        List<Roles> rolesList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Roles as roles");
            rolesList = (List<Roles>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rolesList;
    }

    public List getRolesById(Integer idRole) {
        List<Roles> rolesAccionList = null;

        try {
            Transaction tx = session.beginTransaction();
            Query q =
                    session.createQuery("from Roles WHERE id_rol =" + idRole);
            rolesAccionList = (List<Roles>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error:" + e.getMessage());
        } finally {
            session.close();
        }
        return rolesAccionList;
    }

    public int insertRoles(Roles rol) {
        int result = 0;

        try {
            System.out.println("metodo insert");
            session = HibernateSessionFactory.getSession();
            System.out.println("session: " + session);
            System.out.println("Try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            session.save(rol);
            System.out.println("Save");
            ts.commit();
            System.out.println("Se inserto correctamente");
            result = 1;
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            return result;
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            return result;
        } finally {
            session.close();
        }
    }

    public int deleteRoles(Roles rol) throws Exception {
        int result = 0;
        session = HibernateSessionFactory.getSession();
        try {
            Transaction ts = session.beginTransaction();
            session.delete(rol);
            ts.commit();
            System.out.println("Se elimno correctamente");
            result = 1;
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            return result;
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            return result;
        } finally {
            session.close();
        }

    }

    public int updateRol(Roles rol, long id) throws HibernateException, Exception {
        int result = 0;
        try {
            System.out.println("metodo update");
            session = HibernateSessionFactory.getSession();
            System.out.println("try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            Roles rolUpdate = (Roles) session.get(Roles.class, (int) id);
            rolUpdate.setIdRol(rol.getIdRol());
            rolUpdate.setName(rol.getName());
            rolUpdate.setState(rol.getState());
            rolUpdate.setRegistry(rol.getRegistry());

            session.update(rolUpdate);
            System.out.println("Update");
            ts.commit();
            System.out.println("updateRol: " + rolUpdate.toString());
            System.out.println("Se modifico correctamente");
            result = 1;
            return result;

        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            return result;
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            return result;
        } finally {
            session.close();
        }
    }
}
